// Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
// This library is distributed in the hope that it will be useful,
// but without any warranty; without even the implied warranty of merchantability
// or fitness for a particular purpose.
// You should have received a copy of the GNU Lesser General Public License along
// with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
// ---------------------------------------------------------------------------
package pl.bristleback.server.bristle.rights;

import org.apache.log4j.Logger;
import org.jwebsocket.api.WebSocketConnector;
import pl.bristleback.server.bristle.config.BristleConstants;

import java.util.Arrays;
import java.util.Collection;

/**
 * This util provides convenient methods for adding, removing and setting rights or rights sets.
 * <p/>
 * Created on: 2010-09-27 11:46:00 <br/>
 *
 * @author Wojciech Niemiec
 */
public final class ConnectorRightsUtil {
  private static Logger log = Logger.getLogger(ConnectorRightsUtil.class.getName());

  private ConnectorRightsUtil() {
    throw new UnsupportedOperationException();
  }

  /**
   * Awards right given as parameter to given connector. If connector actually posses that right,
   * this method leaves rights set unchanged.
   *
   * @param connector websocket connector.
   * @param right     right.
   */
  public static void addRight(WebSocketConnector connector, String right) {
    ConnectorRightsSet rightsSet = getConnectorRightsSet(connector);
    if (rightsSet == null) {
      throw new IllegalStateException("Rights set not initialized on connector " + connector);
    }
    rightsSet.addRight(right);
  }

  /**
   * Removes given right from connector. If connector doesn't have that right, no operation is performed.
   *
   * @param connector websocket connector.
   * @param right     right.
   */
  public static void removeRight(WebSocketConnector connector, String right) {
    ConnectorRightsSet rightsSet = getConnectorRightsSet(connector);
    if (rightsSet == null) {
      throw new IllegalStateException("Rights set not initialized on connector " + connector);
    }
    rightsSet.removeRight(right);
  }

  /**
   * Creates new empty rights set for connector. If connector actually have rights set,
   * old set is dropped and new one replaces it.
   * todo-wojtek consider if this is effect we want to achieve.
   *
   * @param connector websocket connector
   */
  public static void createRightsSetForConnector(WebSocketConnector connector) {
    ConnectorRightsSet rightsSet = new ConnectorRightsSet();
    connector.setVar(BristleConstants.CONNECTOR_RIGHTS_SET_VARIABLE_NAME, rightsSet);
  }

  private static ConnectorRightsSet getConnectorRightsSet(WebSocketConnector connector) {
    Object rightsSet = connector.getVar(BristleConstants.CONNECTOR_RIGHTS_SET_VARIABLE_NAME);
    return (ConnectorRightsSet) rightsSet;
  }

  /**
   * Checks if connector contains rights given as parameter.
   *
   * @param connector websocket connector.
   * @param requiredRights required rights.
   * @return true if connector posses every right from array.
   */
  public static boolean hasRights(WebSocketConnector connector, String[] requiredRights) {
    if(requiredRights == null){
      requiredRights = new String[0];
    }
    return hasRights(connector, Arrays.asList(requiredRights));
  }

  /**
   * Another way to check whether connector has rights given as a parameter.
   *
   * @param connector      websocket connector
   * @param requiredRights required rights.
   * @return true if connector posses every right from collection.
   */
  public static boolean hasRights(WebSocketConnector connector, Collection<String> requiredRights) {
    ConnectorRightsSet rightsSet = getConnectorRightsSet(connector);
    return (rightsSet != null) && (rightsSet.hasRights(requiredRights));
  }
}
